import axios from "axios";
import store from '@/store'
import { getToken } from '@/utils/auth'
import { Message } from 'element-ui'
const https = axios.create({
  baseURL: "http://127.0.0.1:7002"
  // baseURL: "http://127.0.0.1:7001"
  // baseURL: "http://47.98.109.207:7001"
  // baseURL: "http://eren200.yicp.io"
});

// 添加请求拦截器
https.interceptors.request.use(
  config => {
    if (store.getters.token) {
      // let each request carry token
      // ['X-Token'] is a custom headers key
      // please modify it according to the actual situation
      config.headers['X-Token'] = getToken()
    }
    return config;
  },
  error => {
    return Promise.reject(error);
  }
);

https.interceptors.response.use(
  response => {
    return response.data;
  },
  error => {
    if (error.response.status === 401) {
      Message({
        message: "令牌错误-无权限",
        type: 'error',
        duration: 5 * 1000
      })
      window.vm.$router.push("/login");
    }
    return Promise.reject(error);
  }
);

export default https;
